namespace Plugins.Engine
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Plugins.Interfaces;

    [Serializable]
    public class PluginPropertyCollection<T> : IPluginPropertyCollection<T>, IEnumerable<T>, IEnumerable where T: IPluginProperty
    {
        private readonly Dictionary<string, T> _dictionary;

        public PluginPropertyCollection()
        {
            this._dictionary = new Dictionary<string, T>();
        }

        public PluginPropertyCollection(PluginPropertyCollection<T> dictionary)
        {
            if (dictionary != null)
            {
                this._dictionary = new Dictionary<string, T>(dictionary._dictionary);
            }
            else
            {
                this._dictionary = new Dictionary<string, T>();
            }
        }

        public void Add(T item)
        {
            if (item != null && item.Name != null)
            {
                if (!this._dictionary.ContainsKey(item.Name))
                {
                    this._dictionary.Add(item.Name, item);
                }
            }
        }

        public void Clear()
        {
            this._dictionary.Clear();
        }

        public bool Contains(T item)
        {
            if (item != null && item.Name != null)
            {
                return this._dictionary.ContainsKey(item.Name);
            }
            return false;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            if (array != null && arrayIndex >= 0 && array.Length > arrayIndex)
            {
                this._dictionary.Values.CopyTo(array, arrayIndex);
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            return (IEnumerator<T>) this._dictionary.Values.GetEnumerator();
        }

        public void Remove(T item)
        {
            if (item != null && item.Name != null)
            {
                if (this._dictionary.ContainsKey(item.Name))
                {
                    this._dictionary.Remove(item.Name);
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return this._dictionary.Count;
            }
        }

        public T this[string name]
        {
            get
            {
                if (name != null)
                {
                    if (this._dictionary.ContainsKey(name))
                    {
                        return this._dictionary[name];
                    }
                }
                return default(T);
            }
        }

        public bool ContainsProperty(string name)
        {
            if (name != null)
            {
                if (this._dictionary.ContainsKey(name))
                {
                    return true;
                }
            }
            return false;
        }
    }
}

